/**
 * 
 */
package com.gametime.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.gametime.util.DBUtils;
import com.gametime.util.LogUtil;

/**
 * @author dave
 *
 */
public class LeagueDAO
{

  private int activeSeasonId=0;
  private static LeagueDAO instance=null;
  private LeagueDAO()
  {
    
  }
  
  public static LeagueDAO getInstance()
  {
    if(instance == null)
      instance = new LeagueDAO();
    return instance;
  }
  
  public int getActiveSeason() 
  throws Exception
  {
    if(activeSeasonId ==  0)
      activeSeasonId = fetchActiveSeason();
    return activeSeasonId;
  }
  
  private int fetchActiveSeason()
  throws Exception
  {
    Connection conn=null;
    try
    {
      conn = DBUtils.getInstance().getConnection();
      return fetchActiveSeason(conn);
    }catch(Exception e)
    {
      LogUtil.trace(e);
      throw e;
    }finally 
    {
      DBUtils.closeConnection(conn);
    }
  }
  
  private int fetchActiveSeason(Connection conn)
  throws SQLException
  {
    int result=0;
    final String sql="select season_id from season where active_flag = 'Y'";
    PreparedStatement ps=null;
    ResultSet rs=null;
    try
    {
      ps = conn.prepareStatement(sql);
      rs = ps.executeQuery();
      while(rs.next())
        result = rs.getInt(1);
      
    }catch(SQLException e)
    {
      LogUtil.trace(e);
      throw e;
    }finally 
    {
     rs.close();
     ps.close();
    }
    
    return result;
  }

  /**
   * @param args
   */
  public static void main(String[] args)
  {
    // TODO Auto-generated method stub

  }

}
